# Config panda path
ARCH           = $(shell [ $$(uname -p) = "aarch64" ] && echo arm64_)
PANDA_SDK      = node_modules/@panda/sdk
PANDA_ARCH_BIN = ${PANDA_SDK}/linux_${ARCH}host_tools/bin

# Config command
COMPILER       = ${PANDA_ARCH_BIN}/es2panda
LINKER         = ${PANDA_ARCH_BIN}/ark_link
LAUNCHER       = ${PANDA_ARCH_BIN}/ark

# Config compile and launch flags
COMPILE_FLAGS  = --arktsconfig=./arktsconfig.json
LAUNCH_FLAGS   = --load-runtimes=ets --boot-panda-files=${PANDA_SDK}/ets/etsstdlib.abc

# Config target
TARGET_PATH    = output
TARGET         = ${TARGET_PATH}/output.abc

# Config source
SRC_PATH       = src
SRCS           = $(wildcard ${SRC_PATH}/*.ets)
OBJS           = $(patsubst ${SRC_PATH}/%.ets,${TARGET_PATH}/%.abc,$(SRCS))

# Config entry function of program
ENTRY          = main.ETSGLOBAL::main

run: $(TARGET)
	@echo
	@${LAUNCHER} ${LAUNCH_FLAGS} $(TARGET) ${ENTRY}

$(TARGET): ${OBJS}
	@echo "    linking $@ ..."
	@${LINKER} --output=$@ -- ${OBJS}

${TARGET_PATH}/%.abc: ${SRC_PATH}/%.ets
	@mkdir -p ${TARGET_PATH}
	@echo "    compiling $< ..."
	@${COMPILER} ${COMPILE_FLAGS} --output=$@ $<

clean:
	@rm -rf ${TARGET_PATH}

all: $(TARGET)

.PHONY: all clean run